//
//  MySexViewController.m
//  eshow
//
//  Created by Hisoka on 15/11/17.
//  Copyright © 2015年 Hisoka. All rights reserved.
//

#import "MySexViewController.h"
#import "MySexTableViewCell.h"

static NSString * const MY_SEX_CELL = @"mySexCell";

@interface MySexViewController ()<UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) UITableView *sexTV;
@property (nonatomic, strong) NSArray *cellVal;
@property (nonatomic, strong) NSString *sex;

@end

@implementation MySexViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self customizeBackItem];
    self.navigationController.interactivePopGestureRecognizer.delegate = nil;
    self.automaticallyAdjustsScrollViewInsets = NO;
    
    [self setStyle];
}

- (void) setStyle {
    self.navigationItem.title = @"编辑性别";
    self.view.backgroundColor = GRAY_COLOR;
    
    self.sexTV = [UITableView newAutoLayoutView];
    [self.view addSubview:_sexTV];
    
    [_sexTV autoPinToTopLayoutGuideOfViewController:self withInset:0];
    [_sexTV autoPinEdge:ALEdgeLeft toEdge:ALEdgeLeft ofView:self.view];
    [_sexTV autoPinEdge:ALEdgeRight toEdge:ALEdgeRight ofView:self.view];
    [_sexTV autoPinEdge:ALEdgeBottom toEdge:ALEdgeBottom ofView:self.view];
    
    _sexTV.delegate = self;
    _sexTV.dataSource = self;
    _sexTV.separatorStyle = UITableViewCellSeparatorStyleNone;
    _sexTV.backgroundColor = GRAY_COLOR;
    
    [_sexTV registerClass:[MySexTableViewCell class] forCellReuseIdentifier:MY_SEX_CELL];
    self.cellVal = @[@"保密", @"男", @"女"];
    
    self.sex = [[[TMCache sharedCache] objectForKey:@"userInfo"] objectForKey:@"sex"];
    
}

- (NSInteger) numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 3;
}

- (CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 60;
}

- (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    MySexTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MY_SEX_CELL];
    
    if ([_sex isEqualToString:[NSString stringWithFormat:@"%ld", (long)indexPath.row]]) {
        cell.accessoryType = UITableViewCellAccessoryCheckmark;
    }else{
        cell.accessoryType = UITableViewCellAccessoryNone;
    }
    
    cell.titleLB.text = [_cellVal objectAtIndex:indexPath.row];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    return cell;
}

- (void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [self setUserInfoAction:[NSString stringWithFormat:@"%ld", (long)indexPath.row]];
}

- (void) setUserInfoAction:(NSString *)sex {
    
    NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
    [parameters setObject:[NSString stringWithFormat:@"%@", [[TMCache sharedCache] objectForKey:@"SSID"]] forKey:@"ssid"];
    [parameters setObject:sex forKey:@"sex"];
    
    [[HttpManager instance] requestWithMethod:@"User/upProfile"
                                   parameters:parameters
                                      success:^(NSDictionary *result) {
                                          NSDictionary *userInfoTmp = [result objectForKey:@"data"];
                                          
                                          [[TMCache sharedCache] setObject:userInfoTmp forKey:@"userInfo"];
                                          _sex = [userInfoTmp objectForKey:@"sex"];
                                          
                                          [_sexTV reloadData];

                                      }
                                      failure:^(AFHTTPRequestOperation *operation, NSError *error) {
                                          [SVProgressHUD showInfoWithStatus:[error localizedDescription] maskType:SVProgressHUDMaskTypeBlack];
                                      }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
